package org.pearl.mybatis.demo.test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.pearl.mybatis.demo.dao.UserMapper;
import org.pearl.mybatis.demo.pojo.entity.User;
import org.pearl.mybatis.demo.pojo.qurey.UserQuery;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by TD on 2021/6/13
 */
public class ForeachTest006 {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 动态查询
/*        List<Integer> ids=new ArrayList<>();
        ids.add(1);
        ids.add(10);
        ids.add(14);
        Integer[] integers1 = new Integer[10];
        Integer[] integers = ids.toArray(integers1);
        List<User> userList = userMapper.selectUserListByOrgId(integers);
        System.out.println(userList);*/

/*        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",1);
        map.put("user_name","张三");
        List<User> users = userMapper.selectUserListByMap(map);
        System.out.println(users);*/

        // 批量保存
        List<User> userList=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user =new User();
            user.setUserName("aaab"+i);
            user.setLoginName("bbbb"+i);
            userList.add(user);
        }
        int i = userMapper.insertBatch(userList);
        System.out.println(i);

    }
}
